var contentBag = new Array(); //产品集合，用于保存已选择的产品
$j(document).ready(function () {

	searchContents(true);
});

//重设搜索条件
function resetSearchCriteria(){
	$("sc_keywords").value = "";
	$("sc_excludedKeywords").value = "";
	$("sc_sourceName").value = "";
	$("sc_categoryPaths").selectedIndex = -1;
	$("sc_status").selectedIndex = 0;
}


//搜索文章 isNew标示是否新的搜索,param标示是否自定义参数搜索
function searchContents(isNew,param){
	var postData;
	var url;
	if(isNew){
		if(null==param){
			if(validateSearchCriteria()){
				//有搜索结果搜索
			    postData = $j('#contentSearchCriteria :input').serializeArray();
			    url=__ctxPath+"/content/contentSelector/blank.html?doAction=search&btnSearch=1";
				$j("#contentSelectorResults").load(url,postData,searchContentsCallback);  
		    }else{
		    	return;
		    }
		}else{
			//新的搜索。全部，上架等
			//有搜索结果搜索
			if(param.status  != undefined){
				$j('#storageStatus').val(""+param.status);
			}
			if(param.categoryId  != undefined){
				$j('#storageCategoryId').val(""+param.categoryId);
			}
		    postData = $j('#contentResults :input').serializeArray();
		  	url=__ctxPath+"/content/contentSelector/blank.html?doAction=search&btnSearch=1";
			$j("#contentSelectorResults").load(url,postData,searchContentsCallback); 
		}    
	}else{
		postData = $j('#contentSelectorResults :input').serializeArray();
		url=__ctxPath+"/content/contentSelector/blank.html?doAction=search";
		$j("#contentSelectorResults").load(url,postData,searchContentsCallback);  
	} 
	
}

//分页
function fnOnGoToPage(){
	searchContents(false);
}

//搜索产品回调函数
function searchContentsCallback(){
	if(multiple)$j("#multipleSelectDiv").show();
}
//验证函数
function validateSearchCriteria(){
	if(!validateForm($j('#contentSearchCriteria').get(0))){
    	return false;
   	}
   	return true;
}
//选择多个文章
function selectContents(){
	if(contentBag.length == 0){
		alert(__FMT.contentSelector_pleaseSelect);
		return;
	}
	callbackFunc.call(this,contentBag);
	cleanupContents();
}
//清空文章
function cleanupContents(){
	contentBag = new Array();
	$j("#selectedContentInfo").empty();
}

//选择文章
function selectContent(contentId){
	var content = {"contentId":contentId,
					"contentTitle":$('contentTitle_'+contentId).value,
					"imageUrl":$('imageUrl_'+contentId).value};
					
	if(multiple){
		addContent(content);
	}else{
		callbackFunc.call(this,content);
	}
}
//添加文章
function addContent(content){
	
	if(!isContain(content.contentId)){
		contentBag.push(content);
		addContentInfo(content);
	}else{
		jAlert(__FMT.contentSelector_alreadySelect);
	}	
}
//移除产品
function removeContent(contentId){
	var index = indexOfContentBag(contentId);
	if(-1 != index){
		contentBag.remove(index);
		removeContentInfo(contentId);
	}
}
//获取content在contentBag中的index，不存在返回-1
function indexOfContentBag(contentId){
	for(var i=0; i<contentBag.length; i++){
		var item = contentBag[i];
		if(item.contentId == contentId){
			return i;
		}
	}
	return -1;
}
//判断是否已经包含该产品
function isContain(contentId){
	var isExist = false;
	for(var i=0; i<contentBag.length; i++){
		var item = contentBag[i];
		if(item.contentId == contentId){
			isExist = true;
			break;
		}
	}
	return isExist;
}
//添加文章显示信息 content是一个JSONObject{contentId,contentTitle,imageUrl}
function addContentInfo(content){
	var html = '';
	html += '<li id="selectedContentInfo'+content.contentId+'"><div class="first" style="width:170px;">'+content.contentTitle+'<br/></div>';
	html += '<div class="second"><a href="javascript:void(0)" class="del" onclick="removeContent('+content.contentId+')">&nbsp;</a></div>';    
	html += '</li>';
	$j("#selectedContentInfo").append(html);
	
}
//移除文章显示信息
function removeContentInfo(contentId){
	$j("#selectedContentInfo"+contentId).remove();
}


/*
   *  方法:Array.remove(index)
   *  功能:删除数组元素.
   *  参数:index删除元素的下标.
   *  返回:在原数组上修改数组
   */

 Array.prototype.remove=function(index)
 {
   if(isNaN(index)||index>this.length){return false;}
   for(var i=0,n=0;i<this.length;i++)
   {
       if(this[i]!=this[index])
       {
           this[n++]=this[i]
       }
   }
   this.length-=1
 }

